<?php
/**
 * Link
 */
class controller_link extends controller_base {
	/**
	 * 链接首页
	 */
	public function action_index() {
		$link_category = new model_link_category();
		$link_item = new model_link_item();
		
		$this->assign('tree', $link_item->get_tree());
	}
	
	/**
	 * 精简首页
	 */
	public function action_index2() {
		$link_item = new model_link_item();
		
		$res = $link_item->find_all('1', 'parent_id');

		// 整理数据
		$data = array();
		foreach ($res as $row) {
			if($row['parent_id'] > 0) {
				$data[$row['parent_id']]['data'][] = $row;
				$data[$row['parent_id']]['hits'] += $row['hits'];
			} else {
				$data[$row['link_item_id']]['info'] = $row;
				$data[$row['link_item_id']]['hits'] = $row['hits'];
			}
		}

		// 排序
		foreach ($data as $key => $row) {
			if($row['hits'] < 80) {
				unset($data[$key]);
			} 
		}

		$this->assign('data', $data);
	}

	/**
	 * 编辑链接(以首页为基础)
	 */
	public function action_index_edit() {
		$this->action_index();
	}
	
	/**
	 * 点击轨迹(以首页为基础)
	 */
	public function action_index_track() {
		$link_item = new model_link_item();
		$link_hits = new model_link_hits();

		// 链接数据
		$item_data = array();
		$res_item = $link_item->find_all(1);
		foreach ($res_item as $row) {
			$item_data[$row['link_item_id']] = $row;
		}
		
		// 点击数据
		$today = strtotime(date('Y-m-d'));
		$res_hits = $link_hits->find_all("create_time>='$today'", 'create_time ASC');
		foreach ($res_hits as &$row) {
			$row['create_time'] = date('n/j H:i:s', $row['create_time']);
		}
		
		// 继承首页显示方法
		$this->action_index();
		
		$this->assign('res_hits', $res_hits);
	}
	
	/**
	 * ajax编辑
	 */
	public function action_ajax_edit() {
		$item_id = $_GET['item_id'];
		$link_item = new model_link_item();
		$res = $link_item->find("link_item_id='$item_id'");

		$this->assign('res', $res);
	}
	
	/**
	 * ajax编辑 do
	 */
	public function action_ajax_edit_do() {
		$link_item = new model_link_item();
		$result = $link_item->find("link_item_id={$_POST['link_item_id']}");

		new model_logs('link_item_edit '.print_r($result, 1).print_r($_POST, 1));

		echo $link_item->update($_POST);
	}
	
	/**
	 * 删除链接
	 */
	public function action_delete() {
		$item_id = isset($_GET['item_id']) ? intval($_GET['item_id']) : 0;
		if($item_id === 0) {
			$this->message('没有指定item_id', '?m=link&a=index_edit');
		}

		$link_item = new model_link_item();

		// 原链接数据
		$link_data = $link_item->find("link_item_id=$item_id");

		// 是否有子链接
		$children_data = $link_item->find("parent_id=$item_id AND deleted=0");
		if($children_data) {
			$this->message('请先删除子链接', '?m=link&a=index_edit');
		}

		// 删除
		$link_item->update(array(
			'link_item_id' => $item_id,
			'deleted' => 1,
		));

		// 添加日志
		new model_logs('link_item_delete '.print_r($link_data, 1));

		$this->message('删除成功!', '?m=link&a=index_edit');
	}
	
	/**
	 * 添加分类
	 */
	public function action_cat_add() {
		$link_category = new model_link_category();
		$this->assign('parents', $link_category->get_parents());
	}
	
	/**
	 * 添加分类 do
	 */
	public function action_cat_add_do() {
		$data = $_POST['data'];
		$link_category = new model_link_category();
		$return = $link_category->add($data);
		
		if($return > 0) {
			new model_logs('link_cat_add '.print_r($data, 1));
			$this->message('添加成功', '?m=link&a=cat_add');
		}
	}
	
	/**
	 * 添加链接
	 */
	public function action_item_add() {
		$link_category = new model_link_category();
		$this->assign('cats', $link_category->get_item_cats());
	}
	
	/**
	 * 添加链接 do
	 */
	public function action_item_add_do() {
		$data = $_POST['data'];
		$link_item = new model_link_item();
		$return = $link_item->add($data);
		
		if($return > 0) {
			new model_logs('link_item_add '.print_r($data, 1));
			$this->message('添加成功', '?m=link');
		}
	}
	
	/**
	 * 转到链接
	 */
	public function action_go() {
		// 接收参数
		$item_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

		if($item_id === 0) {
			$this->message('没有指定item_id!', '?m=link');
		}

		// 查询此链接
		$model_item = new model_link_item();
		$link_result = $model_item->find("link_item_id=$item_id");
		if($link_result['url'] == '') {
			$this->message('此链接为空链接', '?m=link');
		}

		$time = time();

		// 记录点击次数
		$time = time();
		mysql_query("INSERT INTO ".DB_PREFIX."link_hits (`link_item_id`, `create_time`) VALUES ('$item_id', '$time')")
		&& 
		mysql_query("UPDATE ".DB_PREFIX."link_item SET hits = hits + 1 WHERE link_item_id = $item_id");
		
		// 跳转
		header("location:{$link_result['url']}");
		exit();
	}
	
	/**
	 * 排行榜
	 */
	public function action_top() {
		$link_item = new model_link_item();
		$link_hits = new model_link_hits();
		$link_category = new model_link_category();
		
		$limit = empty($_GET['limit']) ? 20 : intval($_GET['limit']);
		
		// (1)总点击
		$res_all = $link_item->find_all(1, 'hits DESC', $limit);

		// (1.2) 删除链接点击
		$res_all_deleted = $link_item->find_all('deleted=1', 'hits DESC', $limit);
		
		// (2)72小时
		$time_recent = time() - 86400 * 3;
		$res_recent = $link_hits->find_all("create_time > $time_recent GROUP BY link_item_id", 'hits DESC', $limit, 'link_item_id, COUNT(*) hits');

		// (2.2)30天
		$time_30days = strtotime(date('Y-m-d')) - 29 * 86400;
		$res_recent_30days = $link_hits->find_all("create_time > $time_30days GROUP BY link_item_id", 'hits DESC', $limit, 'link_item_id, COUNT(*) hits');
		
		// (3)正在点击
		$res_hitting = $link_hits->find_all(1, 'create_time DESC', $limit);
		
		// (4)分类排行
		$res_category_top = $link_item->find_all('1 GROUP BY link_cat_id', 'hits DESC', $limit, 'link_cat_id, sum(hits) hits');
		
		// (5)每日点击
		$res_day_hits = $link_hits->find_all('1 GROUP BY date', 'date DESC', $limit, 'count(*) hits, create_time, from_unixtime(create_time, "%Y-%m-%d") date');
		
		// (6)每日点击
		$res_day_top = $link_hits->find_all('1 GROUP BY date', 'hits DESC', $limit, 'count(*) hits, create_time, from_unixtime(create_time, "%Y-%m-%d") date');
		
		// (7)时段点击
		$res_hour_top = $link_hits->find_all('1 GROUP BY hour', 'hits DESC', 24, 'count(*) hits, from_unixtime(create_time, "%k") hour');
		
		$today = strtotime(date('Y-m-d'));
		$res_hour_top2 = $link_hits->find_all("create_time>=$today GROUP BY hour", 'hits DESC', 24, 'count(*) hits, from_unixtime(create_time, "%k") hour');
		
		// (8)上班点击
		$res_work1 =  $link_hits->find_all('FROM_UNIXTIME(create_time, "%k")>=9 AND FROM_UNIXTIME(create_time, "%k")<18 GROUP BY link_item_id', 'hits DESC', $limit, 'link_item_id, count(*) hits');
		
		// (9)下班点击
		$res_work2 = $link_hits->find_all('FROM_UNIXTIME(create_time, "%k")>=18 OR FROM_UNIXTIME(create_time, "%k")<9 GROUP BY link_item_id', 'hits DESC', $limit, 'link_item_id, count(*) hits');

		// (10)分时点击最高
		$res_hour_all_top = $link_hits->find_all('1 GROUP BY hour', 'hits DESC', $limit, 'count(*) hits, create_time, from_unixtime(create_time, "%Y-%m-%d %H") hour');

		// 链接数据
		$item_data = array();
		$res_item = $link_item->find_all(1);
		foreach ($res_item as $row) {
			$item_data[$row['link_item_id']] = $row;
		}
		
		// 分类数据
		$category_data = array();
		$res_category = $link_category->find_all(1);
		foreach ($res_category as $row) {
			$category_data[$row['link_cat_id']] = $row;
		}
		
		// 显示
		$this->assign('res_all', $res_all);
		$this->assign('res_all_deleted', $res_all_deleted);

		$this->assign('res_recent', $res_recent);
		$this->assign('res_recent_30days', $res_recent_30days);

		$this->assign('res_hitting', $res_hitting);
		$this->assign('res_category_top', $res_category_top);
		$this->assign('res_day_hits', $res_day_hits);
		$this->assign('res_day_top', $res_day_top);
		$this->assign('res_hour_top', $res_hour_top);
		$this->assign('res_hour_all_top', $res_hour_all_top);
		$this->assign('res_hour_top2', $res_hour_top2);
		$this->assign('res_work1', $res_work1);
		$this->assign('res_work2', $res_work2);
		
		$this->assign('item_data', $item_data);
		$this->assign('category_data', $category_data);
	}

	/**
	 * 图表
	 */
	public function action_chart() {
		$link_hits = new model_link_hits();

		// (7)时段点击
		$res_hour_top = $link_hits->find_all('1 GROUP BY hour', 'hits DESC', 24, 'count(*) hits, from_unixtime(create_time, "%k") hour');
		
		// 60天每日点击
		$res_day_hits_60 = $link_hits->find_all('1 GROUP BY date', 'date DESC', 60, 'count(*) hits, create_time, from_unixtime(create_time, "%Y-%m-%d") date');

		$this->assign('res_hour_top', $res_hour_top);
		$this->assign('res_day_hits_60', $res_day_hits_60);
	}
	
}
